package com.nanfang51g3.ckz.ui.money.add.bank;

import android.text.TextUtils;

import com.nanfang51g3.ckz.di.ApiModule;
import com.nanfang51g3.ckz.domain.HttpResultBean;
import com.nanfang51g3.ckz.exec.Api2Exception;
import com.nanfang51g3.ckz.http.HttpResultFunc;
import com.nanfang51g3.ckz.http.RestApi;
import com.nanfang51g3.ckz.utils.KLog;
import com.nanfang51g3.ckz.utils.MD5Utils;
import com.nanfang51g3.ckz.utils.SPUtil;

import java.util.TreeMap;

import javax.inject.Inject;

import retrofit2.Retrofit;

/**
 * Created by Administrator on 2016/4/13.
 */
public class AddBankPresenter implements AddBankContract.AddBankActionsListener {

    String message;
    private final RestApi _restApi;
    private final AddBankContract.View mView;

    @Inject
    public AddBankPresenter(RestApi restApi, AddBankContract.View mView) {
        KLog.i("TAG", "进入了...AddFinancialInfoPresenter");
        /**
         * @see ApiModule#provideRestApi(Retrofit)
         */
        this._restApi = restApi;
        KLog.i("TAG", _restApi == null ? "进入了...is null" : "进入了...not null");
        /**
         * @see AddBankPresenterModule#AddBankPresenterModule(AddBankContract.View)
         */
        this.mView = mView;
        KLog.i("TAG", mView == null ? "进入了...is null" : "进入了...not null");
    }

    @Override
    public void addBank(final String real_name, final String bank_card, final String bank_name) {
        TreeMap<String, String> treeMap = new TreeMap<>();
        String userid = (String) SPUtil.get(mView.getContext(), "userid", "");
        if(TextUtils.isEmpty(real_name)){
            mView.showMessage("请输入真实姓名");
            return;
        }
        if(TextUtils.isEmpty(bank_card)){
            mView.showMessage("请输入银行卡号");
            return;
        }
        if(TextUtils.isEmpty(bank_name)){
            mView.showMessage("请输入银行名称");
            return;
        }
        treeMap.put("userid", userid);
        treeMap.put("real_name", real_name);
        treeMap.put("bank_card", bank_card);
        treeMap.put("bank", bank_name);
        String sign = MD5Utils.getAccessToken(treeMap);
        KLog.i("Evd", treeMap.toString() + "-" + sign);
      /*  Observable observable = _restApi.reuserinfoBankCardAndBank(Integer.valueOf(userid), real_name, bank_card, bank_name, sign);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new AddBandResultFunc<UserBean>())
                .subscribe(new SubscriberInterface<UserBean>() {
                    @Override
                    public void onCompleted() {
                        KLog.i("Evd", "onCompleted");

                    }

                    @Override
                    public void _onError(String msg) {
                        KLog.i("Evd", "onError " +msg);
                        *//**
                         * @see  AddBankActivity#showMessage(String)
                         *//*
                        mView.showMessage(msg);
                    }

                    @Override
                    public void onNext(UserBean o) {
                        KLog.i("Evd", "onNext " + o.toString());
                        SPUtil.putAndApply(mView.getContext(), "real_name", real_name);
                        SPUtil.putAndApply(mView.getContext(), "bank_card", bank_card);
                        SPUtil.putAndApply(mView.getContext(), "bank_name", bank_name);
                        *//**
                         * @see  AddBankActivity#showMessage(String)
                         *//*
                        mView.showMessage(message);
                        mView.finalView();
                    }
                });*/
    }

    class AddBandResultFunc<T> extends HttpResultFunc<T> {
        @Override
        public T call(HttpResultBean<T> httpResult) {
            KLog.i("Evd", httpResult == null ? "1-" : "0-" + httpResult.getCode() + "-" + httpResult.getInfo() + "-" + httpResult.getData().toString());
            if (httpResult.getCode() == 2 ||
                    httpResult.getCode() == 3 ||
                    httpResult.getCode() == 4 ||
                    httpResult.getCode() == 5 ||
                    httpResult.getCode() == 6 ||
                    httpResult.getCode() == 7 ||
                    httpResult.getCode() == 8 ||
                    httpResult.getCode() == 9 ||
                    httpResult.getCode() == 10 ||
                    httpResult.getCode() == 11) {
                throw new Api2Exception(httpResult.getInfo());
            }
            message = httpResult.getInfo();
            return httpResult.getData();
        }
    }
}
